package com.example.customview3.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by AD on 2017/4/13.
 */
public class BallView extends View {
    public float currentX = 40;
    public float currentY = 50;
    //定义并创建画笔
    Paint paint = new Paint();

    public BallView(Context context) {
        super(context);
    }

    public BallView(Context context, AttributeSet set) {
        super(context, set);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //设置画笔颜色
        paint.setColor(Color.GREEN);
        canvas.drawCircle(currentX, currentY, 30, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //修改currentX, currentY的值
        currentX = event.getX();
        currentY = event.getY();
        //通知当前组件重绘自身
        invalidate();
        //返回true表明该处理方法已经处理该事件
        return true;
    }
}
